#include "MenuLayer.h"
#include "LevelScene.h"

MenuLayer::~MenuLayer()
{
	CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("gamemenu.plist");
}
CCScene* MenuLayer::scene()
{
	CCScene * scene = NULL;
	do 
	{
		// 'scene' is an autorelease object
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		// 'layer' is an autorelease object
		MenuLayer *layer = MenuLayer::create();

		CC_BREAK_IF(! layer);

		// add layer as a child to scene
		scene->addChild(layer);
	} while (0);

	// return the scene
	return scene;
}



bool MenuLayer::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());

		CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("gamemenu.plist");

		//
		CCMenuItemSprite *pStartItem = CCMenuItemSprite::create(
			CCSprite::createWithSpriteFrameName("menu_resume_normal_CN.png"),
			CCSprite::createWithSpriteFrameName("menu_resume_pressed_CN.png"),
			this,
			menu_selector(MenuLayer::menuStartCallback));

		CCMenuItemSprite *pSettingItem = CCMenuItemSprite::create(
			CCSprite::createWithSpriteFrameName("menu_restart_normal_CN.png"),
			CCSprite::createWithSpriteFrameName("menu_restart_pressed_CN.png"),
			this,
			menu_selector(MenuLayer::menuSettingCallback));

		CCMenuItemSprite *pCloseItem = CCMenuItemSprite::create(
			CCSprite::createWithSpriteFrameName("menu_quit_normal_CN.png"),
			CCSprite::createWithSpriteFrameName("menu_quit_pressed_CN.png"),
			this,
			menu_selector(MenuLayer::menuCloseCallback));

		CCSize size =CCDirector::sharedDirector()->getWinSize();
		CCSize itemSize =pStartItem->getContentSize();
		// Place the menu item bottom-right conner.
		pStartItem->setPosition(ccp(size.width/2, size.height*0.5 + itemSize.height + 10));
		pSettingItem->setPosition(ccp(size.width/2, size.height*0.5 ));
		pCloseItem->setPosition(ccp(size.width/2 , size.height*0.5 -  itemSize.height -5));

		// Create a menu with the "close" menu item, it's an auto release object.
		CCMenu* pMenu = CCMenu::create(pStartItem,pSettingItem,pCloseItem, NULL);
		pMenu->setPosition(CCPointZero);
		CC_BREAK_IF(! pMenu);

		this->addChild(pMenu, 1);

		// 3. Add add a splash screen, show the cocos2d splash image.
		CCSprite* pSprite = CCSprite::createWithSpriteFrameName("menu_bg.png");
		CC_BREAK_IF(! pSprite);

		// Place the sprite on the center of the screen
		pSprite->setPosition(ccp(size.width/2, size.height/2));

		// Add the sprite to HelloWorld layer as a child layer.
		this->addChild(pSprite, 0);

		bRet = true;
	} while (0);

	return bRet;

	return true;
}

void MenuLayer::menuStartCallback(CCObject* pSender)
{
	//CCNode *node =(CCNode*)(&pSender);
	//MenuLayer* layer =MenuLayer::create();
	this->getParent()->removeChild(this);
	cocos2d::CCDirector::sharedDirector()->resume();
}

void MenuLayer::menuSettingCallback(CCObject* pSender)
{

}


void MenuLayer::menuCloseCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->replaceScene(LevelScene::scene());
}

